package com.gacu.viewer.client.pool;

import java.util.ArrayList;

import com.gacu.viewer.client.rna.RNAssDrawable;

public class Pool {

    private final ArrayList<RNAssDrawable> poolObjectList = new ArrayList<RNAssDrawable>();
    private final ArrayList<IPoolObserver> observers      = new ArrayList<IPoolObserver>();

    public void addToPool(RNAssDrawable po) {
        // Add new object to the pool
        poolObjectList.add(po);

        // Notify everyone
        for (IPoolObserver observer : observers) {
            po.addObserver(observer);
        }

        po.notifyObserversAdd();
    }

    public void removeFromPool(RNAssDrawable po) {
        // Remove from the pool
        poolObjectList.remove(po);

        // Notify everyone
        po.notifyObserversDel();
    }

    public void subscribe(IPoolObserver o) {
        observers.add(o);

        for (RNAssDrawable p : poolObjectList) {
            p.addObserver(o);
        }
    }

    public void unsubscribe(IPoolObserver o) {
        for (RNAssDrawable p : poolObjectList) {
            p.removeObserver(o);
        }

        observers.remove(o);
    }
    
    public ArrayList<RNAssDrawable> getAll()
    {
    	ArrayList<RNAssDrawable> res;
    	
    	res = new ArrayList<RNAssDrawable>();
    	
    	res.addAll(poolObjectList);
    	
    	return res;
    	
    }

}
